# -*- coding: utf-8 -*-
#-----------------------------------------
# @Time       : 2018/6/18 9:36
# @Author     : dqz 
# @Email      : 1406990390@qq.com
# @File       : im.py
# @Software   : PyCharm
#-----------------------------------------
import requests

from utils.log import log

from settings import DING_TOKEN,FEISHU_TOKEN,QYWX_TOKEN,IM_TIMEOUT



def send_dingding(self, title, text):
    url = f"https://open.feishu.cn/open-apis/bot/hook/{DING_TOKEN}"
    textMsg = {
        "msgtype": "text",
        "text": {"content": f"{title}:{text}"}
    }
    try:
        requests.post(url, json=textMsg, headers=self.headers, timeout=IM_TIMEOUT)
    except Exception as e:
        log.error(f"send_dingding发送失败：{e}")
        return False
    return True


def send_feishu(self, title, text):
    url = f"https://open.feishu.cn/open-apis/bot/hook/{FEISHU_TOKEN}"
    textMsg = {
        "title": title,
        "text": text
    }
    try:
        requests.post(url, json=textMsg, headers=self.headers, timeout=IM_TIMEOUT)
    except Exception as e:
        log.error(f"send_feishu发送失败：{e}")
        return False
    return True


def send_qiyewx(self, title, text):
    url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={QYWX_TOKEN}"
    textMsg = {
        "msgtype": "text",
        "text": {"content": f"{title}:{text}"}
    }
    try:
        requests.post(url, json=textMsg, headers=self.headers, timeout=IM_TIMEOUT)
    except Exception as e:
        log.error(f"send_qiyewx发送失败：{e}")
        return False
    return True